3
3
.
.
2
2
.
.
2
2
L
L
o
o
g
g
E
E
x
x
e
e
c
c
u
u
t
t
i
i
o
o
n
n
T
T
i
i
m
m
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to use AOP Annotation to Log Method's Execution Time.
Application Schema [Result]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_aop_logging (add Spring Boot Starters from the table)
Edit File: pom.xml (add AOP dependency)
Create Package: log (inside main package)
– Create Class: LogExecutionTime.java (inside log package)
– Create Class: LogExecutionTimeAspect.java (inside log package)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
LogExecutionTime.java
package com.ivoronline.springboot_aop_logging.log;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime { }
http://localhost:8080/Hello
Tomcat
hello()
MyController
@LogExecutionTime
LogExecutionTime
Aspect
LogExecutionTimeAspect.java
package com.ivoronline.springboot_aop_logging.log;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogExecutionTimeAspect {
@Around("@annotation(LogExecutionTime)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
//EXECUTE BEFORE METHOD
long start = System.currentTimeMillis();
//EXECUTE METHOD
Object proceed = joinPoint.proceed();
//EXECUTE AFTER METHOD
long executionTime = System.currentTimeMillis() - start;
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
//RETURN METHOD RESULT
return proceed;
}
}
MyController.java
package com.ivoronline.springboot_aop_logging.controllers;
import com.ivoronline.springboot_aop_logging.log.LogExecutionTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@LogExecutionTime
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
return "Hello from Controller";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
http://localhost:8080/Hello
Console
String com.ivoronline.springboot_aop_logging.controllers.MyController.hello() executed in 10ms
Run Test Class: PersonTest
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>